import 'dart:convert';

import 'package:flutter_ahj_app/base/basewidget/model/BaseModel.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:rxdart/rxdart.dart';

import 'base/ForgetPwdHttpRequest.dart';
import 'bean/StringData.dart';

class ForgetPWDModel extends BaseModel {
  BehaviorSubject<StringData> _getCodeObs;

  Stream<StringData> get getCodeStream => _getCodeObs.stream;

  getCode(String phone) {
    _getCodeObs = new BehaviorSubject();
    ForgetPwdHttpRequest.getCode(phone).listen((result) {
      StringData dataRes = StringData.fromJson(result);
      ToastUtils.showToast(dataRes.msg);
      _getCodeObs.add(dataRes);
    }, onError: (e) {
      //请求异常等
      _getCodeObs.add(null);
      errorData.add(e);
    });
  }

  BehaviorSubject<StringData> _checkCodeObs;

  Stream<StringData> get checkCodeStream => _checkCodeObs.stream;

  checkCode(String phone, String code) {
    _checkCodeObs = new BehaviorSubject();
    ForgetPwdHttpRequest.checkCode(phone, code).listen((result) {
      StringData dataRes = StringData.fromJson(result);
      _checkCodeObs.add(dataRes);
      ToastUtils.showToast(dataRes.msg);
    }, onError: (e) {
      //请求异常等
      _checkCodeObs.add(null);
      errorData.add(e);
    });
  }

  BehaviorSubject<StringData> _resetPwdObs;

  Stream<StringData> get resetPwdStream => _resetPwdObs.stream;

  resetPwd(String phone, String token, String pwd) {
    _resetPwdObs = new BehaviorSubject();
    ForgetPwdHttpRequest.resetPwd(phone, token, pwd).listen((result) {
      StringData dataRes = StringData.fromJson(result);
      _resetPwdObs.add(dataRes);
      if (dataRes.code != 0) ToastUtils.showToast(dataRes.msg);
    }, onError: (e) {
      //请求异常等
      _resetPwdObs.add(null);
      errorData.add(e);
    });
  }

  @override
  void dispose() {
    super.dispose();
    if (_getCodeObs != null) _getCodeObs.close();
    if (_checkCodeObs != null) _checkCodeObs.close();
    if (_resetPwdObs != null) _resetPwdObs.close();
  }
}
